<?php
/**
 * Created by PhpStorm.
 * User: 86131
 * Date: 2020/10/22
 * Time: 15:01
 */
namespace console\controllers;

/*
	* 从交易所取钱
*/

use common\models\Balance;
use common\models\Member;
use common\service\vesrpc\ves;

class GetBalanceFromVesController extends CoreController
{
    //php /www/wwwroot/HUI2/yii get-balance-from-ves/index >> /www/wwwroot/HUI2/logs/get_balance_from_ves.txt
    public function actionIndex()
    {
        $coins = array('USDT','UPH');
        $allMembers = Member::getAll();
        foreach ($allMembers as $member)
        {
            $ves_balance = ves::balance_query($member['member_id']);
            if($ves_balance['error'] != null)
                continue;

            foreach ($coins as $index=>$coin_symbol)
            {
                $index = $index+1;
                $amount = floatval($ves_balance['result'][$coin_symbol]['available']);
                if($amount>0.00001){
                    var_dump('=======从交易所取回 '.$coin_symbol.' 开始转入=====');
                    var_dump('会员id:'.$member['member_id']);
                    var_dump('币种:'.$index);
                    var_dump('数量:'.$amount);
                    try{
                        $transaction = \Yii::$app->db->beginTransaction();
                        $re = Balance::addBalance($member['member_id'],$index,$ves_balance['result'][$coin_symbol]['available'],12,0, '从交易所取回');
                        if (isset($re['state']) && $re['state']==false) continue;
                        $data = ves::balance_update($member['member_id'],$coin_symbol,'GET-'.$coin_symbol,0,-$amount);
                        if($data['error'] != null)
                            throw new \Exception($data['error']['message']);
                        $transaction->commit();
                        var_dump('=======操作成功=====');
                        var_dump('=======转入结束=====');
                    }catch (\Exception $e){
                        $transaction->rollback();
                        var_dump('转入失败 用户ID:'.$member['member_id']);
                        var_dump('=======转入结束=====');
                    }
                }
            }

        }
    }
}